
library(magrittr)

root          <- "/Users/tbrai/Dropbox/DBT/Data/ePOS RCT Jharkhand/ABBA replication package_20200814"
DataDir       <- paste0(root, "/Data/")
AdminDataDir  <- paste0(root, "/Data/Admin/")
OutputDir     <- paste0(here::here(), "/replication/exhibits/")

stock   <- read.csv(paste0(AdminDataDir,"adherence_disbursementstock_compare.csv"))

stock                   <- plm::pdata.frame(stock, index=c("dealer_id_num", "month"))
stock$agg_stock_l1      <- lag(stock$agg_stock, k = 1)
stock$agg_stock_l1_ver2 <- lag(stock$agg_stock, k = 2) + lag(stock$dis_qty, k = 1) - lag(stock$agg_offtake, k = 1)

# gen vars for calculated stock 
stock$stock_calculated      <- stock$agg_stock_l1 + stock$dis_qty - stock$agg_offtake
stock$stock_calculated_ver2 <- stock$agg_stock_l1_ver2 + stock$dis_qty - stock$agg_offtake

stock$stock_calculated_l1      <- lag(stock$stock_calculated, k = 1)
stock$stock_calculated_ver2_l1 <- lag(stock$stock_calculated_ver2, k = 1)

# gen vars for calculated disbursement
stock$dis_calculated <- stock$ent_qty + stock$agg_carryover - stock$stock_calculated_ver2_l1
stock$dis_calculated[stock$dis_calculated < 0] <- 0

stock <- stock %>% dplyr::group_by(admin_dealer_id) %>% 
  dplyr::mutate(lag_carry = dplyr::lag(agg_carryover, n = 1))

# No fixed effects or first differences
fd_no_fe <- lm(dis_qty ~ ent_qty + lag_carry + agg_stock_l1, 
               data = stock, subset = stock$month %in% c(7, 8, 9, 10))

stock <- plm::pdata.frame(stock, index=c("dealer_id_num", "month"))

### FD
fd <- plm::plm(dis_qty ~ -1 + ent_qty + lag_carry + agg_stock_l1, 
               data = stock, model = "fd", index = c("admin_id_num", "month"), subset = stock$month %in% c(7, 8, 9, 10))

fd_jul <- plm::plm(dis_qty ~ -1 + ent_qty + lag_carry + agg_stock_l1, 
                   data = stock, model = "fd", index = c("dealer_id_num", "month"), subset = stock$month %in% c(7, 8))

fd_aug <- plm::plm(dis_qty ~ -1 + ent_qty + lag_carry + agg_stock_l1, 
                   data = stock, model = "fd", index = c("dealer_id_num", "month"), subset = stock$month %in% c(8, 9))

fd_sep <- plm::plm(dis_qty ~ -1 + ent_qty + lag_carry + agg_stock_l1, 
                   data = stock, model = "fd", index = c("dealer_id_num", "month"), subset = stock$month %in% c(9, 10))

texreg::screenreg(l = list(fd_no_fe, fd, fd_jul, fd_aug, fd_sep),
               custom.header = list("\\specialCellCenter{Total \\\\ reconciliation \\\\ period}" = 1,
                                    "\\specialCellCenter{Total \\\\ reconciliation \\\\ period}" = 2,
                                    "Jul - Aug" = 3,
                                    "Aug - Sept" = 4, 
                                    "Sept - Oct" = 5),
               include.rsquared = FALSE, 
               include.adjrs = FALSE,
               custom.model.names = c("(1)", "(2)", "(3)", "(4)", "(5)"),
               custom.coef.map = list("(Intercept)" = "Intercept",
                                      "ent_qty" = "Entitlement", 
                                      "lag_carry" = "Carryover", 
                                      "agg_stock_l1" = "$S_{t-1}$"), 
               custom.gof.rows = list("Estimator" = c("\\specialCellCenter{OLS \\\\ (no fixed effects)}", "FD", "FD", "FD", "FD")),
               caption = "Equation 1 ($D_{t}$)", 
               label = "tab:eq1FPSFE",
               float.pos = "!htbp",
               caption.above = TRUE,
               stars = c(0.1, 0.05, 0.01))

texreg::texreg(l = list(fd_no_fe, fd, fd_jul, fd_aug, fd_sep),
               custom.header = list("\\specialCellCenter{Total \\\\ reconciliation \\\\ period}" = 1,
                                    "\\specialCellCenter{Total \\\\ reconciliation \\\\ period}" = 2,
                                    "Jul - Aug" = 3,
                                    "Aug - Sept" = 4, 
                                    "Sept - Oct" = 5),
               include.rsquared = FALSE, 
               include.adjrs = FALSE,
               custom.model.names = c("(1)", "(2)", "(3)", "(4)", "(5)"),
               custom.coef.map = list("ent_qty" = "Entitlement", 
                                      "lag_carry" = "Carryover", 
                                      "agg_stock_l1" = "$S_{t-1}$"),
               omit.coef = "(Intercept)",
               custom.gof.rows = list("Estimator" = c("OLS", "FPS FEs", "FD", "FD", "FD")),
               caption = "Equation 1 ($D_{t}$)", 
               label = "tab:eq1FD",
               float.pos = "!htbp", 
               custom.note = " ",
               table = FALSE,
               caption.above = TRUE,
               stars = c(0.1, 0.05, 0.01),
               file = paste0(here::here(), "/replication/exhibits/equation1_regression.tex"))
               